<?php
class classifiedApp extends core
{
	public function addAction($arg)
	{
		if (isPost())
		{
			$arg['passdate']	= strtotime($arg['passdate']);
			$arg['posttime']	= time();
			$arg['flag']    	= isset($arg['flag']) ? join(',',$arg['flag']) : '';
			$id					= base::init('classified')->insert($arg);
			if ($arg['audit']) base::init('system')->make('page',$arg['wid'],$arg['classid'],$id,'make',0);
			$this->refresh('添加成功', $arg['referrer']);
		}
		else 
		{
			$template	= $this->template();
			$columns	= base::init('column')->fetchAll(array('wid'=>$arg['wid']));
			$columns	= base::init('column')->listcls($columns, 0);
			$infotype	= base::init('infotype')->fetchAll();
			
			$template->assign('columns', $columns);
			$template->assign('ip', get_real_ip());
			$template->assign('infotype', $infotype);
			$template->display('classified_add.php');
		}
	}
	
	public function editAction($arg)
	{
		if (isPost())
		{
			$arg['passdate']	= strtotime($arg['passdate']);
			$arg['updatetime']	= time();
			$arg['flag']     	= isset($arg['flag']) ? join(',',$arg['flag']) : '';
			$where				= "id={$arg['id']}";
			
			base::init('classified')->update($arg, $where);
			
			if ($arg['audit']) 
			{
				/*
				 * $url = $domain . "/index.php?m=system&a=toMake&wid={$column['wid']}&classid={$column['classid']}&id={$val['id']}&action=page";
            	sfopensocket($url, 0, '', $_COOKIE, FALSE, '', 5, true);

            	$url = $domain . "/index.php?m=system&a=toMake&wid={$column['wid']}&classid={$column['bclassid']}&action=column";
            	sfopensocket($url, 0, '', $_COOKIE, FALSE, '', 5, true);

            	$url = $domain . "/index.php?m=system&a=toMake&wid={$column['wid']}&classid={$column['classid']}&action=column";
            	sfopensocket($url, 0, '', $_COOKIE, FALSE, '', 5, true);

            	$url = $domain . "/index.php?m=system&a=toMake&wid={$column['wid']}&action=index";
            	sfopensocket($url, 0, '', $_COOKIE, FALSE, '', 5, true);
            	*/
				#make($action,$wid,$classid=null,$id=null,$preview='preview',$print=0)
				/*base::init('system')->make(
		    		array('index','column','page'),
		    		$arg['wid'],
		    		isset($arg['bclasssid']) ? $arg['bclassid'] : $arg['classid'],
		    		$arg['id'],
		    		'make',
		    		0
		    	);*/
			}
			
			$this->refresh('修改成功', "index.php?m=column&classid={$arg['classid']}");
		}
		$template	= $this->template();
		$columns	= base::init('column')->fetchAll("wid={$arg['wid']}");
		$columns	= base::init('column')->listcls($columns, 0);
		$infotype	= base::init('infotype')->fetchAll();
		$classified	= base::init('classified')->fetchRow($arg['id']);
		
		$template->assign('ip', get_real_ip());
		$template->assign('columns', $columns);
		$template->assign('infotype', $infotype);
		$template->assign('classified', $classified);
		$template->display('classified_edit.php');
	}
	
	public function setAction($arg)
    {
        if ('html' == $arg['field'])
        {
			if (!is_int($arg['id'])) $arg['id'] = explode(',', $arg['id']);
			
            $where  = array('id'=>$arg['id']);
            $edit   = array('audit'=>$arg['value']);
            
            base::init('classified')->update($edit, $where);
            
            if ($arg['value'] == '1')
            {
            	
            }
            $list		= base::init('classified')->fetchAll($where);
            $classids	= array_get_unique($list,'classid');
            $ids		= array_get_unique($list, 'id');

            $columns	= base::init('column')->fetchAll(array('classid'=>$classids),'','','',false);
            
            $classids	= array();
            foreach ($columns as $column)
            {
            	$classids[]	= $column['bclassid'] ? $column['bclassid'] : $column['classid'];
            	$wid		= $column['wid'];
            }
            
            /*base::init('system')->make(
	    		array('index','column','page'),
	    		$wid,
	    		$classids,
	    		$ids,
	    		'make',
	    		0
	    	);*/
            exit('1');
        }
        else if (in_array($arg['field'], array('h', 'c', 'a')))
        {
            $value  = explode(',', $arg['value']);
            if (!strstr($arg['value'], $arg['field']))
            {
                $value[]    = $arg['field'];
            }
            else 
            {
                foreach ($value as $key => $val)
                {
                    if ($val == $arg['field']) unset($value[$key]);
                }
            }
            $value  = array_filter($value);
            $where  = array('id'=>$arg['id']);
            $edit   = array('flag'=>join(',',$value));
            base::init('classified')->update($edit, $where);
            exit('1');
        }
    }
    
	public function deleteAction($arg)
    {
    	if (!is_int($arg['id'])) $arg['id'] = explode(',', $arg['id']);
    	base::init('classified')->delete(array('id'=>$arg['id']));
    	if ($arg['ajax']) exit('1');
    }
    
    public function unauditAction($arg)
    {
    	if (!isset($arg['wid'])) errorHandle::ParameterErrors('params');

        $show       = $arg['show']	= isset($arg['show']) ? abs($arg['show']) : 20;
        $page       = isset($arg['page']) ? abs($arg['page']) : 1;
        //
        $columns	= base::init('column')->fetchAll();
        
        $classid	= array_get_unique($columns,'classid');

		$website	= base::init('website')->fetchRow(array('wid'=>$arg['wid']));
        $attr		= base::init('website')->getConfig($website['domain']);
        
        $query		= array("classid"=>$classid,"audit"=>0);

        $list		= base::init('classified')->fetchAll($query,'',$show,abs($page-1)*$show);
        $num		= base::init('classified')->getCount(array("classid"=>$classid,"audit"=>0));
        
		$template	= $this->template();

		$template->assign('columns', $columns);
		$template->assign('website', $website);
		$template->assign('attr', $attr);
		$template->assign('list', $list);
		$template->assign('num', $num);
		$template->assign('page', $page);
		$template->assign('show', $show);
		$template->assign('pagecount', ceil($num/$show));
		
    	
    	$template->display('classified_unaudit.php');
    }
}